package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.annotation.PermInfo;
import com.wind.entity.Announcement;
import com.wind.entity.Article;
import com.wind.service.IAnnouncementService;
import com.wind.service.IArticleService;
import com.wind.util.PageUtils;
import com.wind.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@RestController
@RequestMapping("/wind_atc")
public class ArticleController {

        private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

        @Autowired
        private IArticleService articleService;
        @PostMapping
        public Json add(@RequestBody String body) {

            String oper = "add atc";
            log.info("{}, body: {}",oper,body);

            Article atc = JSON.parseObject(body, Article.class);

            if (StringUtils.isEmpty(atc.getTitle())) {
                return Json.fail(oper, "文章标题不能为空");
            }
            if (StringUtils.isEmpty(atc.getImage())) {
                return Json.fail(oper, "图片不能为空");
            }
            if (StringUtils.isEmpty(atc.getLink())){
                return Json.fail(oper, "跳转链接不能为空");
            }
            Article atcDB = articleService.selectOne(new EntityWrapper<Article>().eq("title", atc.getTitle()));
            if (atcDB != null) {
                return Json.fail(oper, "文章已发布");
            }

            //保存新用户数据
            atc.setUpdateTime(new Date());
            atc.setCreateTime(new Date());

            boolean success = articleService.insert(atc);
            return Json.result(oper, success)
                    .data("id",atc.getId())
                    .data("createTime",atc.getCreateTime());
        }

        @PermInfo("删除文章")
        @DeleteMapping
        public Json delete(@RequestBody String body) {

            String oper = "delete atc";
            log.info("{}, body: {}",oper,body);
            Article atc = JSON.parseObject(body, Article.class);
            atc.setIsDelete(1);
            boolean success = articleService.updateById(atc);
            return Json.result(oper, success).data("isDelete",atc.getIsDelete());
        }

        @PostMapping("/query")
        public Json query(@RequestBody String body) {
            String oper = "query atc";
            log.info("{}, body: {}", oper, body);
            JSONObject json = JSON.parseObject(body);
            String title = json.getString("title");
            Wrapper<Article> wrapper=new EntityWrapper<>();
            wrapper.like("title",title);
            Page<Article> page = articleService.selectPage(new Page<>(1,10), wrapper);
            return Json.succ(oper).data("page", page);
        }


        @PermInfo("更新文章的信息")
        @PatchMapping("/info")
        public Json update(@RequestBody String body) {

            String oper = "update atc";
            log.info("{}, body: {}", oper, body);

            Article atc = JSON.parseObject(body, Article.class);

            atc.setUpdateTime(new Date());

            //boolean success = sysUserService.update(user,new EntityWrapper<User>().eq("uid",user.getUid()));
            boolean success = articleService.updateById(atc);

            return Json.result(oper, success).data("updateTime",atc.getUpdateTime());
        }
}
